#!/usr/bin/env python

# -*- coding: utf-8 -*-

import requests

import re
import os
import time

#下载一个 网页

url ='https://www.meegoq.com/book141037.html'

#模拟浏览器发送http请求

response = requests.get(url)

response.encoding ='utf-8'

html = response.text

#小说标题

tittle = re.findall(r'<h1>(.*?)</h1', html)[0]

#print(tittle)

#新建文件保存小说内容

#fb2 =open('%s.txt' % tittle, 'w', encoding ='utf-8')

path = "./%s" % tittle

isExists=os.path.exists(path)

# 判断结果
if not isExists:
# 如果不存在则创建目录
# 创建目录操作函数
	os.makedirs(path)


ul = re.findall(r'<ul class="mulu">.*?</ul>', html, re.S)[0]

#print(ul)
li = re.findall(r'<li class="col3">.*?</li>', ul, re.S)

#print(li)
#exit()

#新建文件保存小说内容

#with open('%s.txt' % tittle) as f:

#循环每个章节，分别下载

n=m=1
for chapter_info in li:
#注意正则表达式易错，.不能代替换行符
    if n > 9:
        #（） 取值的内容为空格内的正则数据
        content_data = re.findall(r'href="(.*?)">(.*?)<', chapter_info)
        #chapter_tittle = chapter_info[1]
        #chapter_url = chapter_info[0]
        chapter_url, chapter_tittle = content_data[0]
        chapter_url ="https:%s" % chapter_url
        print(chapter_url, chapter_tittle)

        #下载章节内容
        chapter_response = requests.get(chapter_url)
        chapter_response.encoding ='utf-8'
        chapter_html = chapter_response.text
        #print(chapter_html)
        #exit()
        #读取章节内容
        chapter_content = re.findall(r'<div class="content" id="content">(.*?)<br/></div>', chapter_html, re.S)[0]
        #print(chapter_content)
        #exit()
        #清洗数据
        chapter_content = chapter_content.replace(' ', '')
        chapter_content = chapter_content.replace('&#12288;', '')
        chapter_content = chapter_content.replace('<br/>', '\r\n')
        #持久化
        #多个 %s 替换 用 （）
        fb =open('%s/%s.txt' % (path,chapter_tittle), 'w', encoding ='utf-8')
        fb.write(chapter_tittle)
        fb.write(chapter_content)
        fb.write('\n')
        #fb2.write(chapter_tittle)
        #fb2.write(chapter_content)
        #fb2.write('\n')
        m=m+1
        if m > 9:
            print('sleep')
            time.sleep(5)
            m = 1
    n = n+1
exit()